package com.jingkong.controller;

import com.jingkong.component.UserComponent;
import com.jingkong.model.User;
import com.jingkong.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/api/users")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserComponent userComponent;

    @GetMapping("/userNatives")
    public List<User> getNative(){
        List<User> usersNative = userComponent.findUsersNative();
        return usersNative;
    }

    @GetMapping("/age-greater-than-and-gender")
    public List<User> getUsersByAgeGreaterThanAndGender(
            @RequestParam Integer age,
            @RequestParam Integer gender) {
        return userService.getUsersByAgeGreaterThanAndGender(age, gender);
    }

    @GetMapping("/age-greater-than")
    public List<User> getUsersByAgeGreaterThan(@RequestParam Integer age) {
        return userService.getUsersByAgeGreaterThan(age);
    }

    @PostMapping("/native")
    public void insertInto(@RequestBody User user){
        userComponent.addUserWithNativeQuery(user.getName(), user.getEmail());
    }

    @GetMapping
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }

    @GetMapping("/{id}")
    public ResponseEntity<User> getUserById(@PathVariable Long id) {
        User user = userService.getUserById(id);
        return ResponseEntity.ok(user);
    }

    @PostMapping
    public User createUser(@RequestBody User user) {
        return userService.createUser(user);
    }

    @PutMapping("/{id}")
    public ResponseEntity<User> updateUser(@PathVariable Long id, @RequestBody User user) {
        User updatedUser = userService.updateUser(id, user);
        return ResponseEntity.ok(updatedUser);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return ResponseEntity.noContent().build();
    }

}
